package bsts.model;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Subscription implements Serializable, Comparable<Subscription> {
	
	private static final long serialVersionUID = 1L;
	
	private long subscriptionId;
	private Date dateCommenced;
	private Date dateTerminated;
	private SubscriptionStatus subscriptionStatus;
	private Plan plan;
	private User regByUser;
	
	public Subscription(long subscriptionId, Date dateCommenced, Date dateTerminated, Plan plan, User user) {
		this.subscriptionId = subscriptionId;
		this.dateCommenced = dateCommenced;
		this.dateTerminated = dateTerminated;
		this.plan = plan;
		this.regByUser = user;
		this.subscriptionStatus = SubscriptionStatus.Activated;
	}

	public long getSubscriptionId() {
		return subscriptionId;
	}

	public void setSubscriptionId(long subscriptionId) {
		this.subscriptionId = subscriptionId;
	}

	public Date getDateCommenced() {
		return dateCommenced;
	}
	
	public String getStrDateCommenced() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		if (getDateCommenced() != null)
			return sdf.format(getDateCommenced());
		return "";
	}

	public void setDateCommenced(Date dateCommenced) {
		this.dateCommenced = dateCommenced;
	}

	public Date getDateTerminated() {
		return dateTerminated;
	}
	
	public String getStrDateTerminated() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		if (getDateTerminated() != null)
			return sdf.format(getDateTerminated());
		return "";
	}

	public void setDateTerminated(Date dateTerminated) {
		this.dateTerminated = dateTerminated;
	}

	public SubscriptionStatus getSubscriptionStatus() {
		return subscriptionStatus;
	}

	public void setSubscriptionStatus(SubscriptionStatus subscriptionStatus) {
		this.subscriptionStatus = subscriptionStatus;
		
		if (this.subscriptionStatus.equals(SubscriptionStatus.Terminated)) {
			setDateTerminated(new Date());
			plan.terminateFeatures();
		}
	}

	public Plan getPlan() {
		return plan;
	}

	public void setPlan(Plan plan) {
		this.plan = plan;
	}

	public User getRegByUser() {
		return regByUser;
	}

	public void setRegByUser(User regByUser) {
		this.regByUser = regByUser;
	}

	@Override
	public int compareTo(Subscription arg0) {
		// TODO Auto-generated method stub
		return Long.valueOf(getSubscriptionId()).compareTo(Long.valueOf(arg0.getSubscriptionId()));
	}
	
}
